//
//  LBMediaPresentViewController.m
//  LangBa
//
//  Created by Mac on 2017/12/21.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BBSharePresentViewController.h"
#import "UIImage+STTools.h"
@interface BBSharePresentViewController ()
@property(nonatomic, strong) STButton                     *clearButton;
@property(nonatomic, strong) UIView                     *customView;
@property(nonatomic, strong) UIImage                     *image;

@end

@implementation BBSharePresentViewController

- (instancetype)init
{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.clearButton = [[STButton alloc] initWithFrame:UIScreenFrame
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                       backgroundImage:nil
                                                 image:nil];
    [weakSelf.clearButton setClicAction:^(UIButton *sender) {
        [weakSelf dismiss];
    }];
    [self.view addSubview:_clearButton];
    
    self.customView = [[UIView alloc] initWithFrame:CGRectMake(0, UIScreenHeight, UIScreenWidth, 342)];
    self.customView.centerX = UIScreenWidth / 2;
    self.customView.top = UIScreenHeight;
    self.customView.backgroundColor = [UIColor whiteColor];
    [self.clearButton addSubview:self.customView];
    UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectedWhiteView)];
    self.customView.userInteractionEnabled = YES;
    [self.customView addGestureRecognizer:ges];
    
    BBUserModel * user = [STUserManger defult].loginedUser;
    //sub
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 40)
                                                     text:@"此二维码已包含邀请\"邀请下级\"等功能\n如未登录，将不能获得奖励"
                                                textColor:FirstTextColor
                                                     font:12
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [self.customView addSubview:titleLable];
    
    
    NSArray * titles = @[@"微信",@"朋友圈",@"QQ",@"QQ空间"];
    NSArray * images = @[@"微信2",@"朋友圈",@"icon_QQ2",@"qqzone-1"];
    CGFloat menuWith = UIScreenWidth / titles.count;
    for (NSInteger i = 0; i < titles.count; i ++) {
        NSString * title = titles[i];
        NSString * imageName = images[i];
        STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 20, menuWith, menuWith)
                                                         title:title
                                                    titleColor:nil
                                                     titleFont:0
                                                  cornerRadius:0
                                               backgroundColor:nil
                                               backgroundImage:nil
                                                         image:[UIImage imageNamed:imageName]];
        buyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [self.customView addSubview:buyButton];
        [buyButton setClicAction:^(UIButton *sender) {
            [weakSelf dismissWithHandle:^{
                if (weakSelf.shareButtonHandle) {
                    weakSelf.shareButtonHandle(sender.currentTitle,weakSelf.image);
                }
               // [SVProgressHUD showInfoWithStatus:[NSString stringWithFormat:@"分享到%@账号正在申请",title]];
            }];
        }];
        buyButton.left = i * menuWith;
        
    }
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, menuWith + 20  + 1  , UIScreenWidth, 1)];
    line.backgroundColor = TM_lineColor;
    [self.customView addSubview:line];
    
    UIColor * titleClolr = FlatOrange;
    if (BBThemeColorManger.isBlack) {
        titleClolr = UIColor.whiteColor;
        self.customView.backgroundColor = BBThemeColorManger.blackStyletableGrayBackColor;
    }
    STButton * cancelButton = [[STButton alloc] initWithFrame:CGRectMake(0, line.bottom, UIScreenWidth, 44)
                                                     title:@"取消"
                                                titleColor:titleClolr
                                                 titleFont:14
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [cancelButton setClicAction:^(UIButton *sender) {
        [weakSelf dismissWithHandle:^{

        }];
    }];
    [self.customView addSubview:cancelButton];
    
    



    self.customView.height = cancelButton.bottom;
    self.customView.top = UIScreenHeight;
    
    CGFloat maxHeight = UIScreenHeight - self.customView.height - 40;
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 0, maxHeight/1.8, maxHeight)];
    CGFloat scale = 1532.0 / 860.0;
    imageView.frame = CGRectMake(0, 0, maxHeight/scale, maxHeight);
//    imageView.frame = CGRectMake(0, 0, 860 / 2, 1532 / 2);
    imageView.centerX = UIScreenWidth / 2;
    imageView.bottom = - 20;
    if (user) {
        [imageView sd_setImageWithURL:[NSURL URLWithString:user.invite_bg] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            self.image = [UIImage st_snapshot:imageView];
        }];
    }else{
        [imageView sd_setImageWithURL:[NSURL URLWithString:[BBSystemManger manger].invite_bg] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            self.image = [UIImage st_snapshot:imageView];
        }];
    }
    
    imageView.backgroundColor = [UIColor clearColor];
    [self.customView addSubview:imageView];
    
    [self configImageView:imageView];
    
}
- (void)configImageView:(UIImageView*)imageView{
    
    BBUserModel * user = [STUserManger defult].loginedUser;
    
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 200, imageView.width, 30)
                                                     text:@"扫码领取"
                                                textColor:[UIColor whiteColor]
                                                     font:18
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.top = imageView.height/2 - 80;
   // [imageView addSubview:titleLable];
    

    UIColor * red = [UIColor colorWithRed:247/255.0 green:0/255.0 blue:137.0/255.0 alpha:1];
    STLabel * tmoneyLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 200, imageView.width, 60)
                                                     text:[BBSystemManger manger].registerAward
                                                textColor:red
                                                     font:50
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    tmoneyLable.top = titleLable.bottom  + 10;
    tmoneyLable.centerY = imageView.height / 2 - 30;
    [imageView addSubview:tmoneyLable];
    
    
    STLabel * desLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 200, imageView.width, 50)
                                                      text:[NSString stringWithFormat:@"%@糖果",
                                                            [BBSystemManger manger].candyName]
                                                 textColor:[UIColor whiteColor]
                                                      font:40
                                               isSizetoFit:NO
                                             textAlignment:NSTextAlignmentCenter];
    desLable.top = tmoneyLable.bottom  ;
  //  [imageView addSubview:desLable];
    
    
    
    UIImageView * codeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 0, 60, 60)];
    codeImageView.right = imageView.width - 10;
    codeImageView.bottom = imageView.height - 25;
    if (user) {
       codeImageView.image = [UIImage st_imageWithDefaultTwoCode:user.share];
    }else{
        NSString * name =  [BBSystemManger manger].share;
        codeImageView.image = [UIImage st_imageWithDefaultTwoCode:name];
    }
   
    [imageView addSubview:codeImageView];
    
    
    STLabel * longLable = [[STLabel alloc] initWithFrame:CGRectMake(0, codeImageView.bottom, 100, 20)
                                                   text:@"长按识别二维码"
                                              textColor:SecendTextColor
                                                   font:8
                                            isSizetoFit:NO
                                          textAlignment:NSTextAlignmentRight];
    longLable.top = codeImageView.bottom ;
    longLable.right = codeImageView.right;
    [imageView addSubview:longLable];
    
    self.image = [UIImage st_snapshot:imageView];
    
}
#pragma mark --Private Method
- (void)show{
    [UIView animateWithDuration:0.6
                          delay:0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.7
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         self.customView.bottom = UIScreenHeight;
                     } completion:^(BOOL finished) {
                         
                     }];
}
- (void)dismiss{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
- (void)dismissWithHandle:(void(^)())handle{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (handle) {
                handle();
            }
        }];
    }];
}
#pragma mark --Action Method

- (void)onSelectedCancleButton{
    
    [self dismiss];
    
}
- (void)onSelectedWhiteView{
    [self.view endEditing:YES];
}

#pragma mark --NetWork Method
- (void)sendQrcodeRequest{
    
}
@end




